import sys
import wx

from gui.todo_list_panels import *
from data.todo_lists_handler import TodoListsHandler, TodoList, TodoListItem

class MainApp(wx.App):
	"""docstring for MainApp"""
	
	def OnInit(self):
		"""docstring for OnInit"""
		self.mainWindow = MainWindow(None, -1, 'Tasks')
		self.mainWindow.app = self
		self.loadTodoLists()
		self.mainWindow.Show()
		
		return True	
		
	def loadTodoLists(self):
		"""docstring for loadTasks"""
		self.tlHandler = TodoListsHandler('./resources/example_database.xml')
		self.todoLists = self.tlHandler.getTodoLists()
		self.mainWindow.loadTodoLists(self.todoLists)
		
			
	def saveTodoLists(self):
		self.tlHandler.todoLists = self.todoLists			
		self.tlHandler.writeTodoLists()

				
class MainWindow(wx.Frame):
	"""hola for MainWindow"""

	def __init__(self, parent, id, title, size=()):
		wx.Frame.__init__(self, parent, id, title, size=(640, 480))
		self.app = None
		self.todoLists = []
		self.positionY = 20
		self.todoListPanels = []
		self.tlIdsPosition = {}
		self.todoListPosition = 0
		self.todoListItemPosition = 0
		
		self.createWindowIcon()
		self.createStatusBar()		
		self.createMenuBar()
		self.createBoxSizer()
		self.createMainPanel()
		self.createSemiToolbar()
		self.createTodoListsContainerPanel()
		
		self.Bind(wx.EVT_CLOSE, self.OnClose)
		
		
	def createWindowIcon(self):
		icon = wx.Icon('./resources/icon.bmp', wx.BITMAP_TYPE_ICO)
		self.SetIcon(icon)
	
	
	def createStatusBar(self):
		"""docstring for createStatusBar"""
		self.statusbar = self.CreateStatusBar(2, wx.ST_SIZEGRIP)
		fields = ["goTodo - Gonzalo Bourdieu @ 2009", "Creceras?"]
		
		i = 0
		for field in fields:
			self.statusbar.SetStatusText(field, i)
			i = i + 1
	
	
	def createMenuBar(self):
		"""docstring for createMenuBar"""
		menubar = wx.MenuBar()
		file = wx.Menu()
		quit = wx.MenuItem(file, 1, "Salir")
		file.AppendItem(quit)
		
		self.Bind(wx.EVT_MENU, self.OnQuit, id=1)
		
		help = wx.Menu()
		about = wx.MenuItem(help, 2, "Acerca de")
		help.AppendItem(about)
		
		self.Bind(wx.EVT_MENU, self.OnAbout, id=2)
		
		menubar.Append(file, "&Archivo")
		menubar.Append(help, "&Ayuda")
		
		self.SetMenuBar(menubar)
	
		
	def createBoxSizer(self):
		"""docstring for createBoxSizer"""
		self.mainSizer = wx.BoxSizer(wx.VERTICAL)
		
	def createMainPanel(self):
		"""docstring for createMainPanel"""
		self.mainPanel = wx.Panel(self, -1)
		self.mainPanel.SetBackgroundColour('#000000')
		self.mainPanel.SetSizer(self.mainSizer)
	
	
	def createSemiToolbar(self):
		"""docstring for createSemiToolbar"""
		self.stSizer = wx.BoxSizer(wx.HORIZONTAL)
		self.semiToolbar = wx.Panel(self.mainPanel, -1, size = (640, 30))
		self.semiToolbar.SetBackgroundColour('#cccccc')
		self.semiToolbar.SetSizer(self.stSizer)
		
		self.btnNewTodoList = wx.Button(self.semiToolbar, -1, label="Nueva lista")
		self.stSizer.Add(self.btnNewTodoList, 0, wx.ALL, 4)
		
		self.btnNewTodoListItem = wx.Button(self.semiToolbar, -1, label="Nuevo item")
		self.stSizer.Add(self.btnNewTodoListItem, 0, wx.ALL, 4)
		
		self.btnNewTodoList.Bind(wx.EVT_BUTTON, self.newTodoList)
		self.btnNewTodoListItem.Bind(wx.EVT_BUTTON, self.newTodoListItem)
		
		self.mainSizer.Add(self.semiToolbar, 0, wx.EXPAND | wx.RIGHT)
	
	
	def createTodoListsContainerPanel(self):
		"""docstring for createTodoListsContainerPanel"""
		
		self.scrollWindowSizer = wx.BoxSizer(wx.VERTICAL)
		
		self.scrollWindow = wx.ScrolledWindow(self.mainPanel, wx.ID_ANY, style=wx.SUNKEN_BORDER)
		self.scrollWindow.SetScrollbars(1, 1, 600, 415)
		self.mainSizer.Add(self.scrollWindow, 1, wx.EXPAND)
		
		self.todoListsSizer = wx.BoxSizer(wx.VERTICAL)
		self.todoListsContainerPanel = wx.Panel(self.scrollWindow, -1, 
												size = (640, 390), 
												pos=(0, 25))
		
		self.scrollWindowSizer.Add(self.todoListsContainerPanel, 1, wx.EXPAND)
		
		self.todoListsContainerPanel.SetBackgroundColour('#ffffff')
		self.todoListsContainerPanel.parent = self
		self.todoListsContainerPanel.SetSizer(self.todoListsSizer)
		
		self.scrollWindow.SetSizer(self.scrollWindowSizer)
	
	
	def newTodoList(self, event):
		"""docstring for newTodoList"""
		newTodoList = TodoList()
		#lastId = self.todoLists[].id
		newTodoList.id = str(len(self.todoLists) + 1)
		self.todoLists.append(newTodoList)
		
		self.addTodoList(newTodoList)
		self.todoListPanels[len(self.todoListPanels) - 1].changeName(None)
			
	
	def newTodoListItem(self, event):
		"""docstring for newTodoListItem"""
		if self.todoListPosition == 0:
			return
		
		todoListPanel = self.getTodoListPanelById(self.todoListPosition)
		todoListPanel.addItem(TodoListItem())
			
	def loadTodoLists(self, todoLists):
		self.todoLists = todoLists
		for todoList in self.todoLists:
			self.addTodoList(todoList)
	
	def addTodoList(self, todoList):
		"""docstring for addTodoList"""		
		self.todoListPanels.append(TodoListPanel(self.todoListsContainerPanel, 
 												 -1, 
 												 todoList))
		 												 
		# 		self.todoListsSizer.Add(self.todoListPanels[len(self.todoListPanels) -1], 
		# 													0,
		# 													wx.EXPAND | wx.ALL,
		# 													1)
													
									
		# separator = wx.StaticLine(self.todoListsContainerPanel)
		# 		separator.SetBackgroundColour('#cccccc')
		# 		separator.SetForegroundColour('#cccccc')
		# 		self.todoListsSizer.Add(separator, 0, wx.EXPAND)
	
	
	def updateTodoListFocus(self, listId):
		"""docstring for updateTodoListFocus"""
		oldListId = self.todoListPosition
		# ahora tengo que avisarle a todos los objetos que vuelvan a ponerse en negro
		for todoListPanel in self.todoListPanels:
			if todoListPanel.todoList.id != listId:
				todoListPanel.resetHeading()
			
		self.todoListPosition = listId
		
	def updateItemFocus(self, itemId):
		"""docstring for updateItemFocus"""
		oldItemId = self.todoListItemPosition
		# ahora tengo que avisarle a todos los objetos que vuelvan a ponerse en negro
		for todoListPanel in self.todoListPanels:
			todoListPanel.resetItemsColors(itemId)
			
		self.todoListItemPosition = itemId
		
	def getTodoListPanelById(self, id):
		for panel in self.todoListPanels:
			if panel.todoList.id == id:
				return panel
		
	def OnAbout(self, event):
		"""docstring for OnAbout"""
		description = """File Hunter is an advanced file manager for the Unix operating 
		system. Features include powerful built-in editor, advanced search capabilities,
		powerful batch renaming, file comparison, extensive archive handling and more.
		"""
		
		licence = """File Hunter is free software; you can redistribute it and/or modify it 
		under the terms of the GNU General Public License as published by the Free Software Foundation; 
		either version 2 of the License, or (at your option) any later version.
		
		File Hunter is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
		without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
		See the GNU General Public License for more details. You should have received a copy of 
		the GNU General Public License along with File Hunter; if not, write to 
		the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA"""
		
		info = wx.AboutDialogInfo()
		info.SetName('Tasks')
		info.SetVersion('0.0.1')
		info.SetDescription(description)
		info.SetCopyright('(C) 2009 Gonzalo Bourdieu')
		info.SetWebSite('http://www.bassobrovelli.com')
		info.SetLicence(licence)
		info.AddDeveloper('Gonzalo Bourdieu')
		info.AddDocWriter('Gonzalo Bourdieu')
		info.AddArtist('The Tango crew')
		info.AddTranslator('Gonzalo Bourdieu')
		
		wx.AboutBox(info)
		
	def OnClose(self, event):
		self.app.saveTodoLists()
		self.Destroy()
	
	def OnQuit(self, event):
		"""docstring for OnQuit"""
		self.Close()
